'use strict';
(function(){
  function hslToHex(h,s,l){s/=100; l/=100; const c=(1-Math.abs(2*l-1))*s, x=c*(1-Math.abs((h/60)%2-1)), m=l-c/2; let r=0,g=0,b=0;
    if(0<=h&&h<60){r=c;g=x;b=0;} else if(60<=h&&h<120){r=x;g=c;b=0;} else if(120<=h&&h<180){r=0;g=c;b=x;}
    else if(180<=h&&h<240){r=0;g=x;b=c;} else if(240<=h&&h<300){r=x;g=0;b=c;} else {r=c;g=0;b=x;}
    r=Math.round((r+m)*255); g=Math.round((g+m)*255); b=Math.round((b+m)*255); const toHex=v=>v.toString(16).padStart(2,'0'); return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase(); }
  function normalizeGrid(grid){
    const items=Array.from(grid.querySelectorAll('.swatch')); if(items.length<18) return;
    let cols=16; const gtc=getComputedStyle(grid).gridTemplateColumns; if(gtc){ const m=gtc.match(/repeat\((\d+)/); cols=m?parseInt(m[1],10)||cols:gtc.split(' ').length||cols; }
    cols=Math.max(6, Math.min(20, cols)); const rows=Math.ceil(items.length/cols); const colorRows=Math.max(3, rows-1);
    const topL=82, botL=34, S=76; const Ls=Array.from({length: colorRows}, (_,r)=> Math.round(topL - (topL-botL)*(r/(colorRows-1))));
    items.forEach((el,i)=>{ const row=Math.floor(i/cols), col=i%cols; let hex; if(row<colorRows){ const h=Math.round((col/cols)*360), l=Ls[row]; hex=hslToHex(h,S,l); } else { const l=Math.round(4 + (col/(cols-1))*92); hex=hslToHex(0,0,l); }
      const orig=el.getAttribute('data-hex')||el.getAttribute('data-color')||el.style.getPropertyValue('--sw')||''; if(orig) el.setAttribute('data-hex-orig', orig);
      el.setAttribute('data-hex', hex); el.style.setProperty('--sw', hex); if(!el.style.background||el.style.background==='') el.style.background=hex;
      const dot=el.querySelector('.dot'); if(dot){ dot.style.setProperty('--sw', hex); dot.style.background=hex; }
    });
  }

  function toHexStringFromBg(bg){
    var m=bg&&bg.match(/rgba?\((\d+),(\d+),(\d+)/);
    if(!m) return null;
    var r=parseInt(m[1],10), g=parseInt(m[2],10), b=parseInt(m[3],10);
    var h=(v)=>('0'+v.toString(16)).slice(-2).toUpperCase();
    return '#'+h(r)+h(g)+h(b);
  }
  function getSwatchHex(el){
    var hex=el.getAttribute('data-hex')||el.getAttribute('data-color')||el.getAttribute('data-swatch');
    if(hex){ hex = hex.toString().trim(); return hex[0]==='#'?hex.toUpperCase():('#'+hex.toUpperCase()); }
    var sw=el.style.getPropertyValue('--sw');
    if(sw){ sw = sw.toString().trim(); return sw[0]==='#'?sw.toUpperCase():('#'+sw.toUpperCase()); }
    var bg=getComputedStyle(el).backgroundColor;
    var hx = toHexStringFromBg(bg);
    return hx;
  }
  function dedupeGrid(grid){
    var seen=new Set();
    Array.from(grid.querySelectorAll('.swatch')).forEach(function(el){
      var hx=getSwatchHex(el);
      if(!hx) return;
      if(seen.has(hx)){
        el.remove();
      }else{
        seen.add(hx);
      }
    });
  }
function run(){ document.querySelectorAll('#color-search .cb-grid, #color-board .cb-grid, #tp-colorboard .cb-grid').forEach(normalizeGrid); }
  document.addEventListener('DOMContentLoaded', run); window.addEventListener('load', run);
  const mo=new MutationObserver(run); mo.observe(document.documentElement,{childList:true,subtree:true});
})();
